/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity;

import db.DBContext;
import db.PGKeys;
import db.RedisKey;
import pgentity.pool.EntityFactory;
import pgentity.pool.PooledEntity;
import share.PGHelper;
import share.TimeUtil;

/**
 *
 * @author Salm
 */
@EntityFactory(factorier = "loadEntity")
public class BuildFriendSnowman implements PooledEntity
{
    private final String uid;
    private final RedisKey redisKey;

    private BuildFriendSnowman(String uid, String dayToken) {
        this.uid = uid;
        
        this.redisKey = PGKeys.USERS.getChild(uid)
                .getChild(PGKeys.FD_PLAY_WITH_FRIEND)
                .getChild(dayToken)
                .getChild(PGKeys.FD_SNOWMAN);
    }
    
    public static BuildFriendSnowman getEntity(String uid, long now)
    {
        String dayToken = TimeUtil.dayToken(now);
        return loadEntity(uid, dayToken);
//        return EntityPool.inst().get(BuildFriendSnowman.class, uid, dayToken);
    }
    
    public static BuildFriendSnowman loadEntity(String uid, String dayToken)
    {
        BuildFriendSnowman bfSnowman = new BuildFriendSnowman(uid, dayToken);
        return bfSnowman;
    }
    
    public static RedisKey key(String uid, String dayToken)
    {
        return PGKeys.USERS.getChild(uid)
                .getChild(PGKeys.FD_PLAY_WITH_FRIEND)
                .getChild(dayToken)
                .getChild(PGKeys.FD_SNOWMAN);
    }
    
    public boolean isHelpedBuild(String fid)
    {
        return PGHelper.toBoolean(DBContext.Redis().sismember(redisKey, fid));
    }
    
    public void helpBuild(String fid)
    {
        DBContext.Redis().saddENX(redisKey, TimeUtil.DAY_SECS, fid);
    }
    
    public int numberFriendHelped()
    {
        return DBContext.Redis().scard(redisKey).intValue();
    }
}
